{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ae0703c6-688c-4dcd-b9b3-f865bf3bf782",
   "metadata": {},
   "source": [
    "# pandas6 - Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "80760da4-63de-45a0-aba6-85867b8815ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84529a18-16b3-4e7f-8155-b402ee88e887",
   "metadata": {},
   "source": [
    "Index是DataFrame和Series的索引。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a138c68a-e277-4d7b-be37-d891507fbc46",
   "metadata": {},
   "source": [
    "## 范围索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0dd9bacd-e2b1-4f9a-8ef5-608bd4a273a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "月份\n",
       "1     929\n",
       "2     512\n",
       "3     991\n",
       "4     840\n",
       "5     765\n",
       "6     634\n",
       "7     608\n",
       "8     717\n",
       "9     775\n",
       "10    687\n",
       "11    967\n",
       "12    811\n",
       "dtype: int32"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales_data = np.random.randint(400, 1000, 12)\n",
    "index = pd.RangeIndex(1, 13, name='月份')\n",
    "ser = pd.Series(data=sales_data, index=index)\n",
    "ser"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cc9685f-9939-48d4-a83e-222033782220",
   "metadata": {},
   "source": [
    "## 分类索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d66bec25-62db-4d86-8572-2f229476ca30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "种类\n",
       "苹果    6\n",
       "香蕉    6\n",
       "苹果    7\n",
       "苹果    6\n",
       "桃子    8\n",
       "香蕉    6\n",
       "dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales_data = [6, 6, 7, 6, 8, 6]\n",
    "index = pd.CategoricalIndex(\n",
    "    data=['苹果', '香蕉', '苹果', '苹果', '桃子', '香蕉'],\n",
    "    categories=['苹果', '香蕉', '桃子'],\n",
    "    ordered=True,\n",
    "    name='种类'\n",
    ")\n",
    "ser = pd.Series(data=sales_data, index=index)\n",
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "55d8e760-9984-44c8-b930-c0b30fa9e458",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "种类\n",
       "苹果    19\n",
       "香蕉    12\n",
       "桃子     8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分组聚合\n",
    "ser.groupby(level=0, observed=True).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "925335cf-5ff7-4ed6-a6a8-62d05d8b8e3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "种类\n",
       "香蕉    12\n",
       "桃子     8\n",
       "苹果    19\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定索引顺序\n",
    "ser.index = index.reorder_categories(['香蕉', '桃子', '苹果'])\n",
    "ser.groupby(level=0, observed=True).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41a21b56-6639-4174-8910-5b8beefe07dd",
   "metadata": {},
   "source": [
    "## 多级索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f0657d8e-0be0-4758-9386-2af236cba217",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([(1,  'red'),\n",
       "            (1, 'blue'),\n",
       "            (2,  'red'),\n",
       "            (2, 'blue')],\n",
       "           names=['no', 'color'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuples = [(1, 'red'), (1, 'blue'), (2, 'red'), (2, 'blue')]\n",
    "# 通过元组创建多级索引\n",
    "index = pd.MultiIndex.from_tuples(tuples, names=['no', 'color'])\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "813a9102-2768-4d62-99ab-25b426f6bd59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([(1,  'red'),\n",
       "            (1, 'blue'),\n",
       "            (2,  'red'),\n",
       "            (2, 'blue')],\n",
       "           names=['no', 'color'])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]\n",
    "# 通过数组创建多级索引\n",
    "index = pd.MultiIndex.from_arrays(arrays, names=['no', 'color'])\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f076f44c-483a-4ba4-b84c-2892613634e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "no  color\n",
       "1   red      42\n",
       "    blue     32\n",
       "2   red      86\n",
       "    blue     12\n",
       "dtype: int32"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用多级索引\n",
    "sales_data = np.random.randint(1, 100, 4)\n",
    "ser = pd.Series(data=sales_data, index=index)\n",
    "ser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "72fe5bb8-7374-488b-ae9c-a162423ac7bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "no\n",
       "1    74\n",
       "2    98\n",
       "dtype: int32"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分组聚合\n",
    "ser.groupby('no').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "727bfd0a-e6c4-4430-b81d-b15d34b4e538",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "color\n",
       "blue     44\n",
       "red     128\n",
       "dtype: int32"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据一级索引分组聚合\n",
    "ser.groupby(level=1).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c3d1f473-2c2c-409b-9e20-bb0d96edb06d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>学号</th>\n",
       "      <th>学期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1001</th>\n",
       "      <th>期中</th>\n",
       "      <td>91</td>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>78</td>\n",
       "      <td>63</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1002</th>\n",
       "      <th>期中</th>\n",
       "      <td>69</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>85</td>\n",
       "      <td>60</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1003</th>\n",
       "      <th>期中</th>\n",
       "      <td>86</td>\n",
       "      <td>72</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>91</td>\n",
       "      <td>69</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1004</th>\n",
       "      <th>期中</th>\n",
       "      <td>91</td>\n",
       "      <td>91</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>64</td>\n",
       "      <td>70</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1005</th>\n",
       "      <th>期中</th>\n",
       "      <td>79</td>\n",
       "      <td>83</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>79</td>\n",
       "      <td>62</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         语文  数学  英语\n",
       "学号   学期            \n",
       "1001 期中  91  73  93\n",
       "     期末  78  63  78\n",
       "1002 期中  69  63  80\n",
       "     期末  85  60  69\n",
       "1003 期中  86  72  89\n",
       "     期末  91  69  69\n",
       "1004 期中  91  91  93\n",
       "     期末  64  70  69\n",
       "1005 期中  79  83  82\n",
       "     期末  79  62  93"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_ids = np.arange(1001, 1006)\n",
    "semisters = ['期中', '期末']\n",
    "# 创建多级索引\n",
    "index = pd.MultiIndex.from_product((stu_ids, semisters), names=['学号', '学期'])\n",
    "courses = ['语文', '数学', '英语']\n",
    "scores = np.random.randint(60, 101, (10, 3))\n",
    "df = pd.DataFrame(data=scores, columns=courses, index=index)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "09a31ead-0dfe-4600-ba6e-6faa43a35bfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>学号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>81.25</td>\n",
       "      <td>65.50</td>\n",
       "      <td>81.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>81.00</td>\n",
       "      <td>60.75</td>\n",
       "      <td>71.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>89.75</td>\n",
       "      <td>69.75</td>\n",
       "      <td>74.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>70.75</td>\n",
       "      <td>75.25</td>\n",
       "      <td>75.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>79.00</td>\n",
       "      <td>67.25</td>\n",
       "      <td>90.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         语文     数学     英语\n",
       "学号                       \n",
       "1001  81.25  65.50  81.75\n",
       "1002  81.00  60.75  71.75\n",
       "1003  89.75  69.75  74.00\n",
       "1004  70.75  75.25  75.00\n",
       "1005  79.00  67.25  90.25"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分组聚合\n",
    "df.groupby(level=0).agg(lambda x: x.values[0] * 0.25 + x.values[1] * 0.75)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad50b662-1ea8-4621-9338-2f52b5ffba4d",
   "metadata": {},
   "source": [
    "## 间隔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "27f313b9-7652-42ad-a621-e2b670361fd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]], dtype='interval[int64, right]')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建间隔索引，区间\n",
    "# 默认左闭右开区间\n",
    "# 通过closed参数可以指定闭区间、左闭右开区间\n",
    "index = pd.interval_range(start=0, end=5)\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f062c741-3c18-4767-8544-98a4d95d8bac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True, False, False, False])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查某个值是否包含在间隔索引划分的区间中\n",
    "index.contains(1.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4261259a-cdbb-442c-a49c-fb18f2efc5ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True,  True,  True, False])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断传入的区间是否和间隔索引划分的区间有重叠\n",
    "index.overlaps(pd.Interval(1.5, 3.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "be13dbf4-e162-4000-bc77-e50c5c7788df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "IntervalIndex([[0, 1), [1, 2), [2, 3), [3, 4), [4, 5)], dtype='interval[int64, left]')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 左闭右开区间\n",
    "index = pd.interval_range(start=0, end=5, closed='left')\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f18db699-2e62-46c6-9afe-c346852129aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "IntervalIndex([[2022-01-01 00:00:00, 2022-01-02 00:00:00],\n",
       "               [2022-01-02 00:00:00, 2022-01-03 00:00:00],\n",
       "               [2022-01-03 00:00:00, 2022-01-04 00:00:00]],\n",
       "              dtype='interval[datetime64[ns], both]')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 闭区间\n",
    "index = pd.interval_range(start=pd.Timestamp('2022-01-01'), end=pd.Timestamp('2022-01-04'), closed='both')\n",
    "index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a07c037-224e-448a-b875-75413bc613fc",
   "metadata": {},
   "source": [
    "## 日期时间索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9115a2ed-bf33-4ec2-bb9d-8ae83ffc4038",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-01', '2021-01-21', '2021-02-10', '2021-03-02',\n",
       "               '2021-03-22', '2021-04-11', '2021-05-01', '2021-05-21',\n",
       "               '2021-06-10', '2021-06-30'],\n",
       "              dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建时间索引\n",
    "pd.date_range('2021-1-1', '2021-6-30', periods=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8177188c-1a4b-447c-84af-b4f7f32a6d8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-03', '2021-01-10', '2021-01-17', '2021-01-24',\n",
       "               '2021-01-31', '2021-02-07', '2021-02-14', '2021-02-21',\n",
       "               '2021-02-28', '2021-03-07', '2021-03-14', '2021-03-21',\n",
       "               '2021-03-28', '2021-04-04', '2021-04-11', '2021-04-18',\n",
       "               '2021-04-25', '2021-05-02', '2021-05-09', '2021-05-16',\n",
       "               '2021-05-23', '2021-05-30', '2021-06-06', '2021-06-13',\n",
       "               '2021-06-20', '2021-06-27'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2021-1-1', '2021-6-30', \n",
    "              freq='W') # 采样周期为一周，周日为一周的开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "99a7b9e2-edf2-40b9-bec0-362ed4909db2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-01', '2021-01-08', '2021-01-15', '2021-01-22',\n",
       "               '2021-01-29', '2021-02-05', '2021-02-12', '2021-02-19',\n",
       "               '2021-02-26', '2021-03-05', '2021-03-12', '2021-03-19',\n",
       "               '2021-03-26', '2021-04-02', '2021-04-09', '2021-04-16',\n",
       "               '2021-04-23', '2021-04-30', '2021-05-07', '2021-05-14',\n",
       "               '2021-05-21', '2021-05-28', '2021-06-04', '2021-06-11',\n",
       "               '2021-06-18', '2021-06-25'],\n",
       "              dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间计算\n",
    "index = pd.date_range('2021-1-1', '2021-6-30', freq='W')\n",
    "# 时间段\n",
    "index - pd.DateOffset(days=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "526e3c7e-8bd2-41b1-9dd8-f8381cc87027",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-03 02:10:00', '2021-01-10 02:10:00',\n",
       "               '2021-01-17 02:10:00', '2021-01-24 02:10:00',\n",
       "               '2021-01-31 02:10:00', '2021-02-07 02:10:00',\n",
       "               '2021-02-14 02:10:00', '2021-02-21 02:10:00',\n",
       "               '2021-02-28 02:10:00', '2021-03-07 02:10:00',\n",
       "               '2021-03-14 02:10:00', '2021-03-21 02:10:00',\n",
       "               '2021-03-28 02:10:00', '2021-04-04 02:10:00',\n",
       "               '2021-04-11 02:10:00', '2021-04-18 02:10:00',\n",
       "               '2021-04-25 02:10:00', '2021-05-02 02:10:00',\n",
       "               '2021-05-09 02:10:00', '2021-05-16 02:10:00',\n",
       "               '2021-05-23 02:10:00', '2021-05-30 02:10:00',\n",
       "               '2021-06-06 02:10:00', '2021-06-13 02:10:00',\n",
       "               '2021-06-20 02:10:00', '2021-06-27 02:10:00'],\n",
       "              dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index + pd.DateOffset(hours=2, minutes=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f5521588-d210-4ac3-a1f8-95043a8ae19a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>148.91</td>\n",
       "      <td>149.9606</td>\n",
       "      <td>144.950</td>\n",
       "      <td>149.10</td>\n",
       "      <td>2330166.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-08</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-13</th>\n",
       "      <td>155.62</td>\n",
       "      <td>157.6400</td>\n",
       "      <td>152.310</td>\n",
       "      <td>152.51</td>\n",
       "      <td>3271577.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-18</th>\n",
       "      <td>150.94</td>\n",
       "      <td>157.4300</td>\n",
       "      <td>149.610</td>\n",
       "      <td>152.94</td>\n",
       "      <td>3187153.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-09</th>\n",
       "      <td>123.26</td>\n",
       "      <td>124.1100</td>\n",
       "      <td>119.585</td>\n",
       "      <td>119.99</td>\n",
       "      <td>3470483.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-14</th>\n",
       "      <td>119.46</td>\n",
       "      <td>120.3500</td>\n",
       "      <td>117.530</td>\n",
       "      <td>119.22</td>\n",
       "      <td>2527860.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-19</th>\n",
       "      <td>114.14</td>\n",
       "      <td>114.6000</td>\n",
       "      <td>111.190</td>\n",
       "      <td>112.08</td>\n",
       "      <td>2059607.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-24</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-29</th>\n",
       "      <td>112.81</td>\n",
       "      <td>116.0600</td>\n",
       "      <td>111.300</td>\n",
       "      <td>115.10</td>\n",
       "      <td>1454617.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>73 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              Open      High      Low   Close     Volume\n",
       "Date                                                    \n",
       "2022-01-03  148.91  149.9606  144.950  149.10  2330166.0\n",
       "2022-01-08     NaN       NaN      NaN     NaN        NaN\n",
       "2022-01-13  155.62  157.6400  152.310  152.51  3271577.0\n",
       "2022-01-18  150.94  157.4300  149.610  152.94  3187153.0\n",
       "2022-01-23     NaN       NaN      NaN     NaN        NaN\n",
       "...            ...       ...      ...     ...        ...\n",
       "2022-12-09  123.26  124.1100  119.585  119.99  3470483.0\n",
       "2022-12-14  119.46  120.3500  117.530  119.22  2527860.0\n",
       "2022-12-19  114.14  114.6000  111.190  112.08  2059607.0\n",
       "2022-12-24     NaN       NaN      NaN     NaN        NaN\n",
       "2022-12-29  112.81  116.0600  111.300  115.10  1454617.0\n",
       "\n",
       "[73 rows x 5 columns]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baidu_df = pd.read_excel('data/2022年股票数据.xlsx', sheet_name='BIDU', index_col='Date')\n",
    "baidu_df.sort_index(inplace=True)\n",
    "# 每5天记录一次数据\n",
    "baidu_df.asfreq('5D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "637dbdad-88fa-4fb5-9e28-4998d4cb5fa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-31</th>\n",
       "      <td>151.834250</td>\n",
       "      <td>155.501470</td>\n",
       "      <td>148.755630</td>\n",
       "      <td>152.183000</td>\n",
       "      <td>3.498542e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-02-28</th>\n",
       "      <td>157.680263</td>\n",
       "      <td>161.643947</td>\n",
       "      <td>155.390863</td>\n",
       "      <td>158.938947</td>\n",
       "      <td>2.688915e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-31</th>\n",
       "      <td>143.395217</td>\n",
       "      <td>148.291517</td>\n",
       "      <td>138.510143</td>\n",
       "      <td>142.973043</td>\n",
       "      <td>6.411250e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-04-30</th>\n",
       "      <td>130.035250</td>\n",
       "      <td>132.492250</td>\n",
       "      <td>126.301830</td>\n",
       "      <td>128.803000</td>\n",
       "      <td>3.579267e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-31</th>\n",
       "      <td>121.388571</td>\n",
       "      <td>124.888419</td>\n",
       "      <td>118.335552</td>\n",
       "      <td>121.821429</td>\n",
       "      <td>3.322147e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-06-30</th>\n",
       "      <td>145.988095</td>\n",
       "      <td>148.762329</td>\n",
       "      <td>143.066910</td>\n",
       "      <td>145.682857</td>\n",
       "      <td>3.442716e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-07-31</th>\n",
       "      <td>143.916500</td>\n",
       "      <td>146.410655</td>\n",
       "      <td>140.965030</td>\n",
       "      <td>144.106000</td>\n",
       "      <td>2.078316e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-08-31</th>\n",
       "      <td>137.376087</td>\n",
       "      <td>140.525000</td>\n",
       "      <td>134.869565</td>\n",
       "      <td>137.872174</td>\n",
       "      <td>2.556926e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-30</th>\n",
       "      <td>127.932857</td>\n",
       "      <td>129.994524</td>\n",
       "      <td>126.203410</td>\n",
       "      <td>127.929048</td>\n",
       "      <td>2.257403e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-10-31</th>\n",
       "      <td>101.171529</td>\n",
       "      <td>103.180000</td>\n",
       "      <td>98.575729</td>\n",
       "      <td>100.523810</td>\n",
       "      <td>3.975162e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-11-30</th>\n",
       "      <td>90.401905</td>\n",
       "      <td>92.960662</td>\n",
       "      <td>88.816119</td>\n",
       "      <td>90.919048</td>\n",
       "      <td>3.601258e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>114.969524</td>\n",
       "      <td>117.136900</td>\n",
       "      <td>112.689043</td>\n",
       "      <td>114.680000</td>\n",
       "      <td>3.406129e+06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Open        High         Low       Close        Volume\n",
       "Date                                                                    \n",
       "2022-01-31  151.834250  155.501470  148.755630  152.183000  3.498542e+06\n",
       "2022-02-28  157.680263  161.643947  155.390863  158.938947  2.688915e+06\n",
       "2022-03-31  143.395217  148.291517  138.510143  142.973043  6.411250e+06\n",
       "2022-04-30  130.035250  132.492250  126.301830  128.803000  3.579267e+06\n",
       "2022-05-31  121.388571  124.888419  118.335552  121.821429  3.322147e+06\n",
       "2022-06-30  145.988095  148.762329  143.066910  145.682857  3.442716e+06\n",
       "2022-07-31  143.916500  146.410655  140.965030  144.106000  2.078316e+06\n",
       "2022-08-31  137.376087  140.525000  134.869565  137.872174  2.556926e+06\n",
       "2022-09-30  127.932857  129.994524  126.203410  127.929048  2.257403e+06\n",
       "2022-10-31  101.171529  103.180000   98.575729  100.523810  3.975162e+06\n",
       "2022-11-30   90.401905   92.960662   88.816119   90.919048  3.601258e+06\n",
       "2022-12-31  114.969524  117.136900  112.689043  114.680000  3.406129e+06"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分组聚合，每月记录一次\n",
    "baidu_df.resample('1ME').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a9830fa1-aac5-4216-8e14-8eccff88fafb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">Open</th>\n",
       "      <th colspan=\"2\" halign=\"left\">High</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Low</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Close</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-31</th>\n",
       "      <td>151.834250</td>\n",
       "      <td>5.519929</td>\n",
       "      <td>155.501470</td>\n",
       "      <td>5.074199</td>\n",
       "      <td>148.755630</td>\n",
       "      <td>5.516153</td>\n",
       "      <td>152.183000</td>\n",
       "      <td>5.143787</td>\n",
       "      <td>3.498542e+06</td>\n",
       "      <td>1.241224e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-02-28</th>\n",
       "      <td>157.680263</td>\n",
       "      <td>6.614596</td>\n",
       "      <td>161.643947</td>\n",
       "      <td>5.765534</td>\n",
       "      <td>155.390863</td>\n",
       "      <td>6.839334</td>\n",
       "      <td>158.938947</td>\n",
       "      <td>5.852151</td>\n",
       "      <td>2.688915e+06</td>\n",
       "      <td>5.863052e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-03-31</th>\n",
       "      <td>143.395217</td>\n",
       "      <td>14.600400</td>\n",
       "      <td>148.291517</td>\n",
       "      <td>13.453096</td>\n",
       "      <td>138.510143</td>\n",
       "      <td>14.818202</td>\n",
       "      <td>142.973043</td>\n",
       "      <td>14.399232</td>\n",
       "      <td>6.411250e+06</td>\n",
       "      <td>4.026716e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-04-30</th>\n",
       "      <td>130.035250</td>\n",
       "      <td>11.431935</td>\n",
       "      <td>132.492250</td>\n",
       "      <td>11.029335</td>\n",
       "      <td>126.301830</td>\n",
       "      <td>11.030035</td>\n",
       "      <td>128.803000</td>\n",
       "      <td>11.092042</td>\n",
       "      <td>3.579267e+06</td>\n",
       "      <td>1.120312e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-05-31</th>\n",
       "      <td>121.388571</td>\n",
       "      <td>8.992984</td>\n",
       "      <td>124.888419</td>\n",
       "      <td>9.408567</td>\n",
       "      <td>118.335552</td>\n",
       "      <td>9.295979</td>\n",
       "      <td>121.821429</td>\n",
       "      <td>10.089925</td>\n",
       "      <td>3.322147e+06</td>\n",
       "      <td>1.263279e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-06-30</th>\n",
       "      <td>145.988095</td>\n",
       "      <td>5.165335</td>\n",
       "      <td>148.762329</td>\n",
       "      <td>5.000793</td>\n",
       "      <td>143.066910</td>\n",
       "      <td>5.518726</td>\n",
       "      <td>145.682857</td>\n",
       "      <td>5.777243</td>\n",
       "      <td>3.442716e+06</td>\n",
       "      <td>9.133340e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-07-31</th>\n",
       "      <td>143.916500</td>\n",
       "      <td>4.820930</td>\n",
       "      <td>146.410655</td>\n",
       "      <td>5.184829</td>\n",
       "      <td>140.965030</td>\n",
       "      <td>5.158666</td>\n",
       "      <td>144.106000</td>\n",
       "      <td>5.404786</td>\n",
       "      <td>2.078316e+06</td>\n",
       "      <td>5.093293e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-08-31</th>\n",
       "      <td>137.376087</td>\n",
       "      <td>6.496318</td>\n",
       "      <td>140.525000</td>\n",
       "      <td>6.624626</td>\n",
       "      <td>134.869565</td>\n",
       "      <td>5.697371</td>\n",
       "      <td>137.872174</td>\n",
       "      <td>5.630482</td>\n",
       "      <td>2.556926e+06</td>\n",
       "      <td>1.791440e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-30</th>\n",
       "      <td>127.932857</td>\n",
       "      <td>9.136596</td>\n",
       "      <td>129.994524</td>\n",
       "      <td>8.683950</td>\n",
       "      <td>126.203410</td>\n",
       "      <td>8.759704</td>\n",
       "      <td>127.929048</td>\n",
       "      <td>8.776899</td>\n",
       "      <td>2.257403e+06</td>\n",
       "      <td>5.761210e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-10-31</th>\n",
       "      <td>101.171529</td>\n",
       "      <td>16.425589</td>\n",
       "      <td>103.180000</td>\n",
       "      <td>16.398593</td>\n",
       "      <td>98.575729</td>\n",
       "      <td>16.524724</td>\n",
       "      <td>100.523810</td>\n",
       "      <td>16.367960</td>\n",
       "      <td>3.975162e+06</td>\n",
       "      <td>3.187387e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-11-30</th>\n",
       "      <td>90.401905</td>\n",
       "      <td>7.431792</td>\n",
       "      <td>92.960662</td>\n",
       "      <td>8.235047</td>\n",
       "      <td>88.816119</td>\n",
       "      <td>7.854972</td>\n",
       "      <td>90.919048</td>\n",
       "      <td>8.404363</td>\n",
       "      <td>3.601258e+06</td>\n",
       "      <td>1.239644e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>114.969524</td>\n",
       "      <td>4.754760</td>\n",
       "      <td>117.136900</td>\n",
       "      <td>3.790968</td>\n",
       "      <td>112.689043</td>\n",
       "      <td>3.893223</td>\n",
       "      <td>114.680000</td>\n",
       "      <td>3.440918</td>\n",
       "      <td>3.406129e+06</td>\n",
       "      <td>2.565555e+06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Open                   High                    Low  \\\n",
       "                  mean        std        mean        std        mean   \n",
       "Date                                                                   \n",
       "2022-01-31  151.834250   5.519929  155.501470   5.074199  148.755630   \n",
       "2022-02-28  157.680263   6.614596  161.643947   5.765534  155.390863   \n",
       "2022-03-31  143.395217  14.600400  148.291517  13.453096  138.510143   \n",
       "2022-04-30  130.035250  11.431935  132.492250  11.029335  126.301830   \n",
       "2022-05-31  121.388571   8.992984  124.888419   9.408567  118.335552   \n",
       "2022-06-30  145.988095   5.165335  148.762329   5.000793  143.066910   \n",
       "2022-07-31  143.916500   4.820930  146.410655   5.184829  140.965030   \n",
       "2022-08-31  137.376087   6.496318  140.525000   6.624626  134.869565   \n",
       "2022-09-30  127.932857   9.136596  129.994524   8.683950  126.203410   \n",
       "2022-10-31  101.171529  16.425589  103.180000  16.398593   98.575729   \n",
       "2022-11-30   90.401905   7.431792   92.960662   8.235047   88.816119   \n",
       "2022-12-31  114.969524   4.754760  117.136900   3.790968  112.689043   \n",
       "\n",
       "                            Close                   Volume                \n",
       "                  std        mean        std          mean           std  \n",
       "Date                                                                      \n",
       "2022-01-31   5.516153  152.183000   5.143787  3.498542e+06  1.241224e+06  \n",
       "2022-02-28   6.839334  158.938947   5.852151  2.688915e+06  5.863052e+05  \n",
       "2022-03-31  14.818202  142.973043  14.399232  6.411250e+06  4.026716e+06  \n",
       "2022-04-30  11.030035  128.803000  11.092042  3.579267e+06  1.120312e+06  \n",
       "2022-05-31   9.295979  121.821429  10.089925  3.322147e+06  1.263279e+06  \n",
       "2022-06-30   5.518726  145.682857   5.777243  3.442716e+06  9.133340e+05  \n",
       "2022-07-31   5.158666  144.106000   5.404786  2.078316e+06  5.093293e+05  \n",
       "2022-08-31   5.697371  137.872174   5.630482  2.556926e+06  1.791440e+06  \n",
       "2022-09-30   8.759704  127.929048   8.776899  2.257403e+06  5.761210e+05  \n",
       "2022-10-31  16.524724  100.523810  16.367960  3.975162e+06  3.187387e+06  \n",
       "2022-11-30   7.854972   90.919048   8.404363  3.601258e+06  1.239644e+06  \n",
       "2022-12-31   3.893223  114.680000   3.440918  3.406129e+06  2.565555e+06  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "baidu_df.resample('1ME').agg(['mean', 'std'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "6922b1ae-7b9a-40cd-9051-d9e8696c0fc0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-03 00:00:00+08:00</th>\n",
       "      <td>148.910</td>\n",
       "      <td>149.9606</td>\n",
       "      <td>144.95</td>\n",
       "      <td>149.10</td>\n",
       "      <td>2330166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04 00:00:00+08:00</th>\n",
       "      <td>148.140</td>\n",
       "      <td>148.4289</td>\n",
       "      <td>143.56</td>\n",
       "      <td>146.53</td>\n",
       "      <td>2876800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05 00:00:00+08:00</th>\n",
       "      <td>143.820</td>\n",
       "      <td>150.2600</td>\n",
       "      <td>142.95</td>\n",
       "      <td>143.88</td>\n",
       "      <td>3505931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06 00:00:00+08:00</th>\n",
       "      <td>146.195</td>\n",
       "      <td>153.0000</td>\n",
       "      <td>144.41</td>\n",
       "      <td>150.75</td>\n",
       "      <td>3839019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07 00:00:00+08:00</th>\n",
       "      <td>152.980</td>\n",
       "      <td>157.0000</td>\n",
       "      <td>152.28</td>\n",
       "      <td>153.33</td>\n",
       "      <td>2751971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-23 00:00:00+08:00</th>\n",
       "      <td>113.880</td>\n",
       "      <td>114.2500</td>\n",
       "      <td>111.52</td>\n",
       "      <td>111.61</td>\n",
       "      <td>1221825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-27 00:00:00+08:00</th>\n",
       "      <td>113.100</td>\n",
       "      <td>117.5000</td>\n",
       "      <td>112.48</td>\n",
       "      <td>116.48</td>\n",
       "      <td>2668445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-28 00:00:00+08:00</th>\n",
       "      <td>114.090</td>\n",
       "      <td>115.5300</td>\n",
       "      <td>109.88</td>\n",
       "      <td>111.60</td>\n",
       "      <td>1983757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-29 00:00:00+08:00</th>\n",
       "      <td>112.810</td>\n",
       "      <td>116.0600</td>\n",
       "      <td>111.30</td>\n",
       "      <td>115.10</td>\n",
       "      <td>1454617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-30 00:00:00+08:00</th>\n",
       "      <td>113.490</td>\n",
       "      <td>116.5000</td>\n",
       "      <td>113.15</td>\n",
       "      <td>114.38</td>\n",
       "      <td>1727642</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>251 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                              Open      High     Low   Close   Volume\n",
       "Date                                                                 \n",
       "2022-01-03 00:00:00+08:00  148.910  149.9606  144.95  149.10  2330166\n",
       "2022-01-04 00:00:00+08:00  148.140  148.4289  143.56  146.53  2876800\n",
       "2022-01-05 00:00:00+08:00  143.820  150.2600  142.95  143.88  3505931\n",
       "2022-01-06 00:00:00+08:00  146.195  153.0000  144.41  150.75  3839019\n",
       "2022-01-07 00:00:00+08:00  152.980  157.0000  152.28  153.33  2751971\n",
       "...                            ...       ...     ...     ...      ...\n",
       "2022-12-23 00:00:00+08:00  113.880  114.2500  111.52  111.61  1221825\n",
       "2022-12-27 00:00:00+08:00  113.100  117.5000  112.48  116.48  2668445\n",
       "2022-12-28 00:00:00+08:00  114.090  115.5300  109.88  111.60  1983757\n",
       "2022-12-29 00:00:00+08:00  112.810  116.0600  111.30  115.10  1454617\n",
       "2022-12-30 00:00:00+08:00  113.490  116.5000  113.15  114.38  1727642\n",
       "\n",
       "[251 rows x 5 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间本地化\n",
    "baidu_df = baidu_df.tz_localize('Asia/Chongqing')\n",
    "baidu_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "21126364-6467-48c6-9c0c-7fff03f3847a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-01-02 11:00:00-05:00</th>\n",
       "      <td>148.910</td>\n",
       "      <td>149.9606</td>\n",
       "      <td>144.95</td>\n",
       "      <td>149.10</td>\n",
       "      <td>2330166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03 11:00:00-05:00</th>\n",
       "      <td>148.140</td>\n",
       "      <td>148.4289</td>\n",
       "      <td>143.56</td>\n",
       "      <td>146.53</td>\n",
       "      <td>2876800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04 11:00:00-05:00</th>\n",
       "      <td>143.820</td>\n",
       "      <td>150.2600</td>\n",
       "      <td>142.95</td>\n",
       "      <td>143.88</td>\n",
       "      <td>3505931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05 11:00:00-05:00</th>\n",
       "      <td>146.195</td>\n",
       "      <td>153.0000</td>\n",
       "      <td>144.41</td>\n",
       "      <td>150.75</td>\n",
       "      <td>3839019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06 11:00:00-05:00</th>\n",
       "      <td>152.980</td>\n",
       "      <td>157.0000</td>\n",
       "      <td>152.28</td>\n",
       "      <td>153.33</td>\n",
       "      <td>2751971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-22 11:00:00-05:00</th>\n",
       "      <td>113.880</td>\n",
       "      <td>114.2500</td>\n",
       "      <td>111.52</td>\n",
       "      <td>111.61</td>\n",
       "      <td>1221825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-26 11:00:00-05:00</th>\n",
       "      <td>113.100</td>\n",
       "      <td>117.5000</td>\n",
       "      <td>112.48</td>\n",
       "      <td>116.48</td>\n",
       "      <td>2668445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-27 11:00:00-05:00</th>\n",
       "      <td>114.090</td>\n",
       "      <td>115.5300</td>\n",
       "      <td>109.88</td>\n",
       "      <td>111.60</td>\n",
       "      <td>1983757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-28 11:00:00-05:00</th>\n",
       "      <td>112.810</td>\n",
       "      <td>116.0600</td>\n",
       "      <td>111.30</td>\n",
       "      <td>115.10</td>\n",
       "      <td>1454617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-29 11:00:00-05:00</th>\n",
       "      <td>113.490</td>\n",
       "      <td>116.5000</td>\n",
       "      <td>113.15</td>\n",
       "      <td>114.38</td>\n",
       "      <td>1727642</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>251 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                              Open      High     Low   Close   Volume\n",
       "Date                                                                 \n",
       "2022-01-02 11:00:00-05:00  148.910  149.9606  144.95  149.10  2330166\n",
       "2022-01-03 11:00:00-05:00  148.140  148.4289  143.56  146.53  2876800\n",
       "2022-01-04 11:00:00-05:00  143.820  150.2600  142.95  143.88  3505931\n",
       "2022-01-05 11:00:00-05:00  146.195  153.0000  144.41  150.75  3839019\n",
       "2022-01-06 11:00:00-05:00  152.980  157.0000  152.28  153.33  2751971\n",
       "...                            ...       ...     ...     ...      ...\n",
       "2022-12-22 11:00:00-05:00  113.880  114.2500  111.52  111.61  1221825\n",
       "2022-12-26 11:00:00-05:00  113.100  117.5000  112.48  116.48  2668445\n",
       "2022-12-27 11:00:00-05:00  114.090  115.5300  109.88  111.60  1983757\n",
       "2022-12-28 11:00:00-05:00  112.810  116.0600  111.30  115.10  1454617\n",
       "2022-12-29 11:00:00-05:00  113.490  116.5000  113.15  114.38  1727642\n",
       "\n",
       "[251 rows x 5 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换时区\n",
    "baidu_df.tz_convert('America/New_York')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7045b64-65f4-494f-a8a9-eaf1e8bbacab",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
